package com.mhys.hms.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhys.hms.entity.Employee;
import com.mhys.hms.entity.FeedBackObject;
import com.mhys.hms.service.user.EmpManagerService;

/**
 * 部门管理controller
 * 
 * @author
 *
 */
@Controller
public class EmpManager {
	@Autowired
	private EmpManagerService empManagerService;

	/**
	 * 模糊分页查询
	 * 
	 * @param pageSize
	 * @param pageNumber
	 * @param deptName
	 * @return
	 */
	@RequestMapping(value = "/queryEmpInfo", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject queryEmpInfo(int pageSize, int pageNumber, String deptName) {
		FeedBackObject fbo = new FeedBackObject();

		// 分页查询数据
		List<Employee> list = empManagerService.getEmpInfo(pageSize, pageNumber, deptName);
		// 封装数据
		Map<String, Object> map = new HashMap<String, Object>();
		// 返回总页数
		map.put("total", empManagerService.getTotalNum(pageNumber, pageSize, deptName));
		// 返回总数据
		map.put("data", list);
		fbo.Obj = map;
		return fbo;
	}

	/**
	 * 根据ID删除员工信息
	 * 
	 * @param empID
	 * @return
	 */
	@RequestMapping(value = "/delEmpInfo", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject delEmpInfo(String empID) {
		FeedBackObject fbo = new FeedBackObject();
		int row = empManagerService.delEmpInfo(empID);
		if (row > 0) {
			fbo.success = "OK";
		}
		return fbo;
	}

	/**
	 * 根据id查询员工信息
	 * 
	 * @param empID
	 * @return
	 */
	@RequestMapping(value = "/queryEmpInfoById", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject queryEmpInfoById(String empID) {
		FeedBackObject fbo = new FeedBackObject();
		List<Employee> list = empManagerService.getEmpInfoById(empID);
		fbo.Obj = list;
		return fbo;
	}

	/**
	 * 新增员工信息
	 * 
	 * @param empID
	 * @return
	 */
	@RequestMapping(value = "/addEmpInfo", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public FeedBackObject addEmpInfo(HttpServletRequest req) {
		FeedBackObject fbo = new FeedBackObject();
		Employee emp = new Employee();
		emp.setBirthAddr(req.getParameter("birthdayAddr"));
		emp.setDepartName(req.getParameter("departName"));
		emp.setEducation(req.getParameter("education"));
		emp.setEmpAge(Integer.parseInt(req.getParameter("empAge")));
		emp.setEmpBirthday(initDate(req.getParameter("empBirthday")));
		emp.setEmpID(req.getParameter("empID"));
		emp.setEmpName(req.getParameter("empName"));
		emp.setHomeAddr(req.getParameter("homeAddr"));
		String id = req.getParameter("id");
		if(id == null || "".equals(id)) {
			emp.setId(null);
		}else {
			emp.setId(Integer.parseInt(id));
		}
		emp.setProfessional(req.getParameter("professional"));
		emp.setJobType(req.getParameter("jobType"));
		emp.setJoinTime(initDate(req.getParameter("joinTime")));
		emp.setMarryStatus(req.getParameter("marryStatus"));
		emp.setNation(req.getParameter("nation"));
		emp.setPhone(req.getParameter("phone"));
		emp.setPoliticalStatus(req.getParameter("politicalStatus"));
		emp.setRemark(req.getParameter("remark"));
		emp.setSchool(req.getParameter("school"));
		emp.setSex(req.getParameter("sex"));
		emp.setSignName(req.getParameter("signName"));
		emp.setSignTime(initDate(req.getParameter("signTime")));
		
		int row = empManagerService.addEmpInfo(emp);
		//如果大于0，说明新增成功
		if (row > 0) {
			fbo.success = "OK";
		}
		return fbo;
	}
	
	//日期格式化
	public Date initDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}

}
